package com.exercise.widgets.datetime

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import androidx.core.util.Pair
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.databinding.ActivityDateTimeBinding
import com.exercise.widgets.datetime.DateTimeActivity
import com.exercise.widgets.datetime.picker.DatePickerFragment
import com.exercise.widgets.datetime.picker.TimePickerFragment
import com.google.android.material.datepicker.MaterialDatePicker

class DateTimeActivity : BaseActivity<BaseViewModel, ActivityDateTimeBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityDateTimeBinding
        get() = ActivityDateTimeBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        binding.datePicker.setOnClickListener {
            showDatePicker()
        }
        binding.dateRangePicker.setOnClickListener {
            showDateRangePicker()
        }
        binding.timePickerDialog.setOnClickListener {
            TimePickerFragment().show(supportFragmentManager, "timePicker")
        }
        binding.datePickerDialog.setOnClickListener {
            DatePickerFragment().show(supportFragmentManager, "datePicker")
        }
    }

    private fun showDatePicker() {
        val picker = MaterialDatePicker.Builder.datePicker()
            .setTitleText("Select Date")
            .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
            .setInputMode(MaterialDatePicker.INPUT_MODE_TEXT)
            .build()

        picker.addOnPositiveButtonClickListener {

        }
        picker.addOnNegativeButtonClickListener {

        }
        picker.show(supportFragmentManager, "datePicker")
    }

    private fun showDateRangePicker() {
        val dateRangePicker = MaterialDatePicker.Builder.dateRangePicker()
            .setTitleText("Select Date")
            .setSelection(
                Pair(
                    MaterialDatePicker.thisMonthInUtcMilliseconds(),
                    MaterialDatePicker.todayInUtcMilliseconds()
                )
            )
            .build()
        dateRangePicker.show(supportFragmentManager, "dateRangePicker")
    }

    companion object {
        fun start(context: Context) {
            Intent(context, DateTimeActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}